<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            设备故障提报
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">厂房位置</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入厂房位置" id="locationName" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">图示位置</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入图示位置" id="areaName" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">资产班组</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入资产班组" id="geographiclocation" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <!-- <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备名称</div>
                    <select id="duty">
                    </select>
                </div> -->
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备编号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择设备" id="equipCode" value="" onclick="toNewPage()" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="toNewPage()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <!-- <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备名称</div>
                    <select id="duty">
                    </select>
                </div> -->
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备型号<span style="color:red">*</span></div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="设备型号" id="equipName" value="" onclick="toNewPage()" />
                    </div>
                </div>
            </li>
            
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">维修类型<span style="color:red">*</span></div>
                    <select id="equipFaultClass">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">故障描述<span style="color:red">*</span></div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入故障描述" id="description" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
           
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">是否造成停台<span style="color:red">*</span></div>
                    <select id="isStop">
                        <option value=''>--请选择--</option>
                        <option value='是'>是</option>
                        <option value='否'>否</option>
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">是否紧急<span style="color:red">*</span></div>
                    <select id="isUrgent">
                        <option value=''>--请选择--</option>
                        <option value='是'>是</option>
                        <option value='否'>否</option>
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">首选维修人员</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择首选维修人员" id="jxPerson" value="" onclick="toPersonPage()" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="toPersonPage()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报修人<span style="color:red">*</span></div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入报修人" id="bxPerson" value="" onclick="getIdSelector()" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">联系方式<span style="color:red">*</span></div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入联系方式" id="mobile" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">派单描述</div>
                    <div class="aui-list-item-input">
                        <textarea id="remark" onclick="getIdSelector();"></textarea>
                    </div>
                </div>
            </li> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">备注</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="remark" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报废时间</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择时间" class="aui-input" id="scrapDate">
                    </div>
                </div>
            </li> -->
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <!-- <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="getJxPerson()">随机机修人员</div> -->
    </div>
    <!-- <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer> -->
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var equipId = "";//设备ID
    var personCode = "";
    //初始化
    apiready = function() {

        $("#toolSeqence").focus();
        idSelector = "toolSeqence";

        //初始化当前日期
        // crateDate();
        $("#isStop").val("否");
        $("#isUrgent").val("否");
        //查询维修类型下拉框
        getEquipFaultCombox();

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //获取当前日期
    function crateDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#scrapDate").val(year + "-" + month + "-" + day);
    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择时间'
        }, function(ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#scrapDate").val(date);
            }
        });
    }

    //查询维修类型下拉框
    function getEquipFaultCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUdiList";
        var data = {
            values: {
                code: 'EQUIP_FAULT_CLASS',
                removeDefault: 'Y',//去掉请选择
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    //生产线
                    var list = eval(myobj.list);
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }
                    $api.html($api.byId("equipFaultClass"), html);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //报废记录保存
    function save() {
        
        //派单描述
        // var remark = $("#remark").val();
        var description = $("#description").val();
        //故障描述非空校验
        if (description == null || description == "" || description == undefined) {
            api.toast({
                msg: "请输入故障描述!",
                duration: 3000
            });
            return;
        }
        if (personCode == null || personCode == "" || personCode == undefined) {
            getJxPerson('save')
        }else{
            doSave()
        }
        
    }

    //报废记录保存
    function doSave() {
               
        //设备ID
        var equip = equipId;
        //维修类别
        var faultText = $("#equipFaultClass").val();
        //故障描述
        var description = $("#description").val();
        //联系方式
        var mobile = $("#mobile").val();
        //是否停台
        var isStop = $("#isStop").val();
        //是否紧急
        var isUrgent = $("#isUrgent").val();
        //首选人员
        var person = $("#person").val();
        //设备型号
        var equipName = $("#equipName").val();
        var bxPerson = $("#bxPerson").val();

        //派单描述
        // var remark = $("#remark").val();

        //设备非空校验
        if (equipName == null || equipName == "" || equipName == undefined) {
            api.toast({
                msg: "请输入设备型号!",
                duration: 3000
            });
            return;
        }
        //维修类别非空校验
        if (faultText == null || faultText == "" || faultText == undefined) {
            api.toast({
                msg: "请输入维修类别!",
                duration: 3000
            });
            return;
        }
        //故障描述非空校验
        if (description == null || description == "" || description == undefined) {
            api.toast({
                msg: "请输入故障描述!",
                duration: 3000
            });
            return;
        }
        //联系方式非空校验
        if (mobile == null || mobile == "" || mobile == undefined) {
            api.toast({
                msg: "请输入联系方式!",
                duration: 3000
            });
            return;
        }
        //是否停台非空校验
        if (isStop == null || isStop == "" || isStop == undefined) {
            api.toast({
                msg: "请输入是否停台!",
                duration: 3000
            });
            return;
        }
        //是否紧急非空校验
        if (isUrgent == null || isUrgent == "" || isUrgent == undefined) {
            api.toast({
                msg: "请输入是否紧急!",
                duration: 3000
            });
            return;
        }
        // //首选人员非空校验
        // if (person == null || person == "" || person == undefined) {
        //     api.toast({
        //         msg: "请选择首选人员!",
        //         duration: 3000
        //     });
        //     return;
        // }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveEquipmentFault";
        var data = {
            values: {
                equip : equip,
                faultText  : faultText,
                description  : description,
                equipName  : equipName,
                mobile  : mobile,
                isStop  : isStop,
                isUrgent  : isUrgent,
                jxPerson  : personCode,
                bxPerson : bxPerson,
                // remark : remark,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    equipId = '';
                    personCode = '';
                    //清空表单
                    $("#equipCode").val("");
                    $("#equipName").val("");
                    $("#faultText").val("");
                    $("#description").val("");
                    $("#mobile").val("");
                    $("#isStop").val("");
                    $("#isUrgent").val("");
                    $("#jxPerson").val("");
                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //赋值
            $("#" + idSelector).val(ret);

        });
    }

    //刀具序列号回车事件
    $("#toolSeqence").on('keypress', function(e) {
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#toolSeqence").val(sm);

        }
    });

    function toNewPage() {
        var locationName = $("#locationName").val();
        var areaName = $("#areaName").val();
        var geographiclocation = $("#geographiclocation").val();
        api.openWin({
            name: 'emEquipmentList',
            url: 'emEquipmentList.html',
            pageParam: {
                name: "emEquipmentFaultLb_pt",
                locationName: locationName,
                areaName: areaName,
                geographiclocation: geographiclocation,
            }
        });
    }

    //选择带回回调函数
    function setEquip(sysid,code,name,locationName, areaName, zcwz) {
        equipId = sysid;
        $("#equipCode").val(code);
        $("#equipName").val(name);
        $("#locationName").val(locationName);
        $("#areaName").val(areaName);
        $("#geographiclocation").val(zcwz);
    }

    function toPersonPage() {
        api.openWin({
            name: 'emEquipmentPersonList',
            url: 'emEquipmentPersonList.html',
            pageParam: {
                name: "emEquipmentFaultLb_pt",
            }
        });
    }
    //选择带回回调函数
    function setPerson(sysid,code,name) {
        personCode = code;
        $("#jxPerson").val(name);

    }

    function getJxPerson(type){
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getSxjxPerson";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    if(list && list.length > 0){
                        personCode = list[0].code;
                        $("#jxPerson").val(list[0].name);
                    }else{
                        // api.toast({
                        //     msg: "当前没有空闲机修人员!",
                        //     duration: 3000
                        // });
                        // return;
                    }
                    
                    //失败
                } else if (myobj.code == "1") {
                    // api.toast({
                    //     msg: "当前没有空闲机修人员!",
                    //     duration: 3000
                    // });
                    // return;
                }

                if(type){
                    doSave()
                }
            }
        });
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
